<?php
/**
 * 积分 记录
 */

namespace app\admin\model;


use app\common\model\Model;
use think\Db;
use app\api\model\PointsRecord as ApiPointRecord;
use app\api\model\ExchangePrize;
use app\api\model\ExchangeRecord;
class PointsRecord extends Model
{
    const TABLE_NAME = 'points_record';
    const CACHE_EXPIRE_LEVEL = self::CACHE_EXPIRE_LEVEL_1;

    protected $name = self::TABLE_NAME;

   
    public static function getRecordQuery($user_id=null)
    {
       $query = self::commonQuery(self::TABLE_NAME);
        
        if(is_numeric($user_id))
        {
            $query->where(['record_user_id'=>$user_id]);
        }else if(is_array($user_id))
        {
            $query->where(['record_user_id'=>['in',$user_id]]);
        }
        return $query;
    }
    
    public static function handleRecordInfo($info)
    {
        
        $info['record_addtime']=date('Y-m-d H:i',$info['record_addtime']);
        $info['user_name']=UserModel::getUserName($info['record_user_id']);

        switch ($info['record_type'])
        {
            case ApiPointRecord::POINTS_INVITE_LEVEL_1:
            case ApiPointRecord::POINTS_INVITE_LEVEL_2:
            case ApiPointRecord::POINTS_INVITE_LEVEL_3:
            case ApiPointRecord::POINTS_INVITE_MASTER:
                 $info['record_item']='邀请用户奖励积分';
                 $info['record_points'] ='+'.$info['record_points'];
                break;
            case ApiPointRecord::POINTS_REGISTER:
                $info['record_item']='注册奖励积分';
                $info['record_points'] ='+'.$info['record_points'];
                break;
            case ApiPointRecord::POINTS_SIGN_IN:
                $info['record_item']='每天签到一次';
                $info['record_points'] ='+'.$info['record_points'];
                break;
            case ApiPointRecord::POINTS_GOODS:

                $goods_info = GoodsModel::getGoodsInfoById(OrderModel::getGoodsId($info['record_item_id']));
                $info['record_item']='购买商品 '.$goods_info['goods_name'];
                $info['record_points'] ='+'.$info['record_points'];
                break;
            case ApiPointRecord::POINTS_EXCHANGE:
                $exchange_info = ExchangeRecord::getRecordInfo($info['record_item_id']);
                $prize_info = ExchangePrize::getPrizeInfo($exchange_info['record_prize_id']);
                $info['record_item']='兑换一次'.$prize_info['prize_name'];
                $info['record_points'] ='-'.$info['record_points'];
                break;
            case ApiPointRecord::POINTS_WITHDRAW:
                $info['record_item']='提现';
                $info['record_points'] ='-'.$info['record_points'];
                break;
            case ApiPointRecord::POINTS_WITHDRAW_REFUND:
                $info['record_item']='提现退还';
                $info['record_points'] ='+'.$info['record_points'];
                break;
            case ApiPointRecord::POINTS_ORDER_CANCEL:
                $info['record_item']='订单取消扣除';
                $info['record_points'] ='-'.$info['record_points'];
                break;
            case ApiPointRecord::POINTS_MONTH_SIGN_IN:
                $info['record_item']='连续签到一个月赠送';
                $info['record_points'] ='+'.$info['record_points'];
                break;
            case ApiPointRecord::POINTS_THREE_HUNDRED_DAY_SIGN_IN:
                $info['record_item']='连续签到300天赠送';
                $info['record_points'] ='+'.$info['record_points'];
                break;
            case ApiPointRecord::POINTS_DONATION:
                $info['record_item']='捐赠积分';
                $info['record_points'] ='-'.$info['record_points'];
                break;
            default:
                $info['record_item']='';
                $info['record_points'] ='+'.$info['record_points'];
                break;
        }

            
        return $info;
    }
    
}